/*
 * 文件名：IdApiController.java 版权：Copyright by bonc 描述： 修改人：蔡强 修改时间：2016年8月4日 跟踪单号： 修改单号： 修改内容：
 */

package com.bonc.hbase.api.ctrl;


import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bonc.hbase.api.client.HbaseConvertUtil;
import com.bonc.hbase.api.client.HbaseOperateClient;
import com.bonc.hbase.api.client.RowkeyUtil;
import com.bonc.hbase.api.constant.BaseConstant;
import com.bonc.hbase.api.entity.JsonResult;
import com.bonc.hbase.api.util.ValidateRequestUtil;


/**
 * 根据手机查询userId接口类
 * 
 * @author caiqiang
 * @version 2016年8月4日
 * @see UserApiController
 * @since
 */
@RestController
@RequestMapping("/api/v1")
public class UserApiController {
    /**
     * HBase 数据操作客户端
     */
    @Autowired
    private HbaseOperateClient hbaseOperateClient;

    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(UserApiController.class);

    /**
     * Description: 根据手机查询userId接口
     * 
     * @param phone
     * @param request
     * @return Object
     */
    @RequestMapping(value = {"/userId"}, method = RequestMethod.GET)
    public Object getId(String phone, HttpServletRequest request) {

        if (!ValidateRequestUtil.validate(request)) {
            return new JsonResult(BaseConstant.CODE_TOKEN_ILLEGAL, BaseConstant.MSG_TOKEN_ILLEGAL);
        }

        if (StringUtils.isEmpty(phone)) {
            return new JsonResult(BaseConstant.CODE_PARAM_ERROR, BaseConstant.MSG_PARAM_ERROR);
        }

        byte[] rowkey = RowkeyUtil.generateHashRowkey(phone);
        Result result = new Result();
        try {
            result = hbaseOperateClient.getResult(rowkey, "userinfo");
        }
        catch (IOException e) {
            LOG.error(e.getMessage());
            return new JsonResult(BaseConstant.CODE_ERROR, BaseConstant.MSG_ERROR);
        }

        if (result == null || result.isEmpty()) {
            return new JsonResult(BaseConstant.CODE_DATA_NONEXIST, BaseConstant.MSG_DATA_NONEXIST);
        }

        String[] strValueArray = HbaseConvertUtil.getListValue(new String(result.getValue(
            Bytes.toBytes("f"), Bytes.toBytes("q"))));

        if (strValueArray == null || strValueArray.length <= 0) {
            return new JsonResult(BaseConstant.CODE_DATA_NONEXIST, BaseConstant.MSG_DATA_NONEXIST);
        }

        String userId = strValueArray[0];
        JSONObject returnObj = new JSONObject();
        returnObj.put("userId", userId);

        return new JsonResult(returnObj);
    }
}
